package com.maixy.studyonlineapi.apis;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.maixy.studyonlineapi.bean.Page;
import com.maixy.studyonlineapi.bean.SessionUser;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Date 2020/7/26 22:35
 * @Created by maixy
 */
@Data
@Slf4j
public class ApiRequest implements Serializable {
    private Map<String, Object> data;

    private String name;

    private SessionUser user;

    private String teamId;

    public Object getDataParam(String name) {
        if (null == data || null == name) {
            return null;
        }
        return data.get(name);
    }

    public String getDataParamAsString(String name) {
        return getDataParam(name) != null ? getDataParam(name).toString() : null;
    }

    public String getDataParamAsString(String name, String defaultValue) {
        return (getDataParam(name) != null && StringUtils.isNotBlank(getDataParamAsString(name))) ? getDataParam(name).toString() : defaultValue;
    }

    public Boolean getDataParamAsBool(String name) {
        return StringUtils.isNoneBlank(getDataParamAsString(name)) ? Boolean.parseBoolean(getDataParam(name).toString()) : null;
    }

    public Boolean getDataParamAsBool(String name, Boolean defaultValue) {
        return StringUtils.isNoneBlank(getDataParamAsString(name)) ? Boolean.parseBoolean(getDataParam(name).toString()) : defaultValue;
    }

    public Integer getDataParamAsInt(String name) {
        return StringUtils.isNotBlank(getDataParamAsString(name)) ? new BigDecimal(getDataParamAsString(name)).intValue() : null;
    }

    public Integer getDataParamAsInt(String name, Integer defaultValue) {
        return StringUtils.isNotBlank(getDataParamAsString(name)) ? new BigDecimal(getDataParamAsString(name)).intValue() : defaultValue;
    }

    public Float getDataParamAsFloat(String name) {
        return StringUtils.isNotBlank(getDataParamAsString(name)) ? new BigDecimal(getDataParamAsString(name)).floatValue() : null;
    }

    public Double getDataParamAsDouble(String name) {
        return StringUtils.isNotBlank(getDataParamAsString(name)) ? new BigDecimal(getDataParamAsString(name)).doubleValue() : null;
    }

    public Long getDataParamAsLong(String name) {
        try {
            return StringUtils.isNotBlank(getDataParamAsString(name)) ? Long.valueOf(getDataParamAsString(name)) : null;
        } catch (Exception e) {
            return null;
        }
    }

    public Long getDataParamAsLong(String name, Long defaultValue) {
        try {
            return StringUtils.isNotBlank(getDataParamAsString(name)) ? Long.valueOf(getDataParamAsString(name)) : defaultValue;
        } catch (Exception e) {
            return defaultValue;
        }
    }

    @SuppressWarnings("unchecked")
    public <T> T getDataParamAsType(String name, Class<T> clazz) {
        Object o = this.getDataParam(name);
        if (clazz.isInstance(o)) {
            return (T) o;
        } else if (o instanceof JSONObject) {
            try {
                return JSON.parseObject(((JSONObject) o).toJSONString(), clazz);
            } catch (Exception e) {
                log.error("Get data param as type error", e);
                return null;
            }
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public <T> List<T> getDataParamAsList(String name, Class<T> clazz) {
        Object o = this.getDataParam(name);
        List<T> list = new ArrayList<T>();
        if (clazz.isInstance(o)) {
            list.add((T) o);
            return list;
        } else if (o instanceof JSONArray) {
            try {
                JSONArray arrs = (JSONArray) o;
                for (Object arr : arrs) {
                    list.add(JSON.parseObject(JSON.toJSONString(arr, SerializerFeature.DisableCircularReferenceDetect), clazz));
                }
                return list;
            } catch (Exception e) {
                return null;
            }
        } else if (o instanceof ArrayList) {
            return (List<T>) o;
        }
        return null;
    }

    public Page getPage() {
        Map<String, Object> map = this.getData();
        if (null == map) {
            return null;
        }
        JSONObject jsonObject = (JSONObject) map.get("page");
        if (null == jsonObject) {
            Page page = new Page();
            page.setPage(1);
            page.setSize(10);
            return page;
        }
        Page page = JSONObject.toJavaObject(jsonObject, Page.class);
        if (page.getPage() < 0) {
            page.setPage(1);
        }
        if (page.getSize() < 0) {
            page.setSize(10);
        }
        if (page.getLastValue() == null) {
            page.setLastValue(0L);
        }
        return page;
    }
}
